package elitech.vietnam.weatherfashion.tabs.today;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.koushikdutta.urlimageviewhelper.UrlImageViewHelper;

import elitech.vietnam.weatherfashion.R;
import elitech.vietnam.weatherfashion.backgroundtask.PostLikeTask;
import elitech.vietnam.weatherfashion.config.Const;
import elitech.vietnam.weatherfashion.config.GlobalValue;
import elitech.vietnam.weatherfashion.config.Utils;
import elitech.vietnam.weatherfashion.dialog.ConfirmDialog;
import elitech.vietnam.weatherfashion.dto.MemberDTO;
import elitech.vietnam.weatherfashion.dto.PostDTO;
import elitech.vietnam.weatherfashion.tabs.AbstractFragment;
import elitech.vietnam.weatherfashion.tabs.BaseContainerFragment;
import elitech.vietnam.weatherfashion.widget.LikeButton;

public class TodayPagerFragment extends AbstractFragment implements View.OnClickListener, View.OnTouchListener {

	int page;

	private ImageView iv_image;
	private LikeButton button_like;
	private RelativeLayout layout_mask;
	private PostDTO data;
	
	private int					fingerState			= FINGER_RELEASED;
	private final static int	FINGER_RELEASED		= 0;
	private final static int	FINGER_TOUCHED		= 1;
	private final static int	FINGER_DRAGGING		= 2;
	private final static int	FINGER_UNDEFINED	= 3;
	
	public static TodayPagerFragment init(int val, PostDTO data) {
		TodayPagerFragment frm = new TodayPagerFragment();
		Bundle args = new Bundle();
		args.putInt("page", val);
		frm.setArguments(args);
		return frm;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		page = getArguments().getInt("page");
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.today_item, container, false);
		setupView(view);

		layout_mask = (RelativeLayout) view.findViewById(R.id.today_mask_layout);
		iv_image = (ImageView) view.findViewById(R.id.today_viewpager_image);
		button_like = (LikeButton) view.findViewById(R.id.today_button_like);
		
		button_like.setUp(true);
		
		return view;
	}
	
	@Override
	public void onResume() {
		super.onResume();
		initViews();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.today_mask_layout:
			((BaseContainerFragment) getParentFragment().getParentFragment()).replaceFragment(TodayDetailFragment.init(this.data), true);
			break;
		case R.id.today_button_like:
			button_like.setEnabled(false);
			MemberDTO user = GlobalValue.getInstance(getActivity()).readUserInfo();
			if (user == null) {
				Utils.openLogin(getActivity());
			} else {
				String ms = getString(R.string.do_you_want_to_like_this_post_);
				if (data.isLiked())
					ms = getString(R.string.do_you_want_to_unlike_this_post_);
				new ConfirmDialog.Builder(getActivity())
				.setMessage(ms)
				.setYesClick(new ConfirmDialog.OnYesClick() {
					@Override
					public void doClick() {
						new PostLikeTask(getActivity(), button_like, data, !data.isLiked()).execute();
					}
				})
				.build()
				.show();
			}
			button_like.setEnabled(true);
			break;
		default:
			break;
		}
	}

	// Setup view data
	public void initViews() {
		if (this.data == null)
			return;
		if (button_like != null) {
			button_like.setData(data.getLikes(), data.isLiked());
			button_like.setOnClickListener(this);
		}
		if (iv_image != null) {
			UrlImageViewHelper.setUrlDrawable(iv_image, Const.SERVER_IMAGE_URL + this.data.getImage(), R.drawable.loading_time);
			layout_mask.setOnClickListener(this);
//			iv_image.setOnTouchListener(this);
		}
	}

	public int getPageIndex() {
		return page;
	}

	public void setPageData(PostDTO data) {
		this.data = data;
		notifyDataChanged();
	}
	
	private void notifyDataChanged() {
		View view = getView();
		if (view != null) {
			iv_image = (ImageView) view.findViewById(R.id.today_viewpager_image);
			button_like = (LikeButton) view.findViewById(R.id.today_button_like);
			if (button_like != null) {
				button_like.setUp(true);
				button_like.setData(data.getLikes(), data.isLiked());
				button_like.setOnClickListener(this);
			}
			if (iv_image != null) {
				UrlImageViewHelper.setUrlDrawable(iv_image, Const.SERVER_IMAGE_URL + this.data.getImage(), R.drawable.loading_time);
				layout_mask.setOnClickListener(this);
//				iv_image.setOnTouchListener(this);
			}
		}
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		if (v.getId() == R.id.today_viewpager_image) {
			switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				if (fingerState == FINGER_RELEASED)
					fingerState = FINGER_TOUCHED;
				else
					fingerState = FINGER_UNDEFINED;
				break;
			case MotionEvent.ACTION_UP:
				if (fingerState != FINGER_DRAGGING) {
					fingerState = FINGER_RELEASED;
					((BaseContainerFragment) getParentFragment().getParentFragment()).replaceFragment(TodayDetailFragment.init(this.data), true);
				} else if (fingerState == FINGER_DRAGGING)
					fingerState = FINGER_RELEASED;
				else
					fingerState = FINGER_UNDEFINED;
				break;
			case MotionEvent.ACTION_MOVE:
				if (fingerState == FINGER_TOUCHED || fingerState == FINGER_DRAGGING)
					fingerState = FINGER_DRAGGING;
				else
					fingerState = FINGER_UNDEFINED;
				break;
			default:
				fingerState = FINGER_UNDEFINED;
			}
			return false;
		}
		return true;
	}
}
